package com.violet.detective.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.violet.detective.entity.Repair;
import com.violet.detective.search.RepairSearchParameter;
import com.violet.detective.service.RepairService;
import com.violet.detective.util.ERCode;
import com.violet.detective.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/repair")
public class RepairController {
    @Resource
    private RepairService repairService;

    @PostMapping("/add")
    public R addRepair(@RequestBody Repair repair) {
        System.out.println(repair);
        return repairService.addRepair(repair) ? R.ok() : R.error();
    }

    @PostMapping("/update")
    public R updateRepair(@RequestBody Repair repair) {
        boolean result = repairService.editRepair(repair);
        return result ? R.ok() : R.error();
    }

    @DeleteMapping("/deleteRepair/{id}")
    public R deleteRepair(@PathVariable Integer id) {
        boolean isSuccess = repairService.deleteRepair(id);
        return isSuccess ? R.ok() : R.error();
    }

    @GetMapping("/getRepairById/{id}")
    public R getRepairById(@PathVariable Integer id) {
        return R.ok(repairService.getRepairById(id));
    }

    @PostMapping("/search")
    public R searchRepairs(@RequestBody RepairSearchParameter searchParameter) {
        List<Repair> result = repairService.searchRepairs(searchParameter);
        return R.ok(result);
    }

    @PostMapping("/page-search")
    public R searchRepairs(
            @RequestBody RepairSearchParameter searchParameter,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size
    ) {
        Page<Repair> resultPage = repairService.searchRepairs(searchParameter, page, size);
        return R.ok(resultPage);
    }

    @GetMapping("/repairs")
    public R findAllRepairs() {
        List<Repair> result = repairService.findAllRepairs();
        return R.ok(result);
    }

    @GetMapping("/page-repairs")
    public R findAllRepairs(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size
    ) {
        Page<Repair> resultPage = repairService.findAllRepairs(page, size);
        return R.ok(resultPage);
    }

//    @GetMapping("/searchByEquipmentId")
//    public R findByEquipmentId(@RequestParam int equipmentId) {
//        List<Repair> result = repairService.findByEquipmentId(equipmentId);
//        return result.isEmpty() ? R.error(ERCode.SEARCH__ERR) : R.ok(result);
//    }
//
//    @GetMapping("/searchByEquipmentName")
//    public R findByEquipmentName(@RequestParam String equipmentName) {
//        List<Repair> result = repairService.findByEquipmentName(equipmentName);
//        return result.isEmpty() ? R.error(ERCode.SEARCH__ERR) : R.ok(result);
//    }
//
//    @GetMapping("/searchByType")
//    public R findByType(@RequestParam String type) {
//        List<Repair> result = repairService.findByType(type);
//        return result.isEmpty() ? R.error(ERCode.SEARCH__ERR) : R.ok(result);
//    }
//
//    @GetMapping("/searchByLubricationPart")
//    public R findByLubricationPart(@RequestParam String lubricationPart) {
//        List<Repair> result = repairService.findByLubricationPart(lubricationPart);
//        return result.isEmpty() ? R.error(ERCode.SEARCH__ERR) : R.ok(result);
//    }
//
//    @GetMapping("/searchByOilSupplyType")
//    public R findByOilType(@RequestParam String oilType) {
//        List<Repair> result = repairService.findByOilSupplyType(oilType);
//        return result.isEmpty() ? R.error(ERCode.SEARCH__ERR) : R.ok(result);
//    }
//
//    @GetMapping("/searchByLubricationMethod")
//    public R findByLubricationMethod(@RequestParam String lubricationMethod) {
//        List<Repair> result = repairService.findByLubricationMethod(lubricationMethod);
//        return result.isEmpty() ? R.error(ERCode.SEARCH__ERR) : R.ok(result);
//    }

}
